import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

// 链配置数据
const chainConfigs = [
  {
    chain: 'ethereum',
    isEnabled: true,
    rpcUrl: 'https://ethereum.publicnode.com',
    wssUrl: 'wss://ethereum.publicnode.com',
    minValueUsd: 5000.00,
    blockTime: 12000,
    maxRetries: 3,
    requestDelay: 1000,
    batchSize: 100,
    fallbackRpcs: [
      'https://ethereum.publicnode.com',
      'https://eth.llamarpc.com',
      'https://rpc.ankr.com/eth',
      'https://ethereum-rpc.publicnode.com'
    ],
    errorCount: 0,
    totalTxCount: 0
  },
  {
    chain: 'bsc',
    isEnabled: true,
    rpcUrl: 'https://bsc-dataseed.binance.org/',
    minValueUsd: 5000.00,
    blockTime: 3000,
    maxRetries: 3,
    requestDelay: 1000,
    batchSize: 100,
    fallbackRpcs: [
      'https://bsc-dataseed.binance.org/',
      'https://bsc-dataseed1.defibit.io/',
      'https://bsc-dataseed1.ninicoin.io/'
    ],
    errorCount: 0,
    totalTxCount: 0
  },
  // 可以添加其他链配置...
];

// 基础监控代币数据 (精简版，只包含最重要的代币)
const essentialTokens = [
  { symbol: 'BTC', name: 'Bitcoin', coingeckoId: 'bitcoin', isActive: true, minValueUsd: 10000 },
  { symbol: 'ETH', name: 'Ethereum', coingeckoId: 'ethereum', isActive: true, minValueUsd: 5000 },
  { symbol: 'USDT', name: 'Tether', coingeckoId: 'tether', isActive: true, minValueUsd: 50000 },
  { symbol: 'USDC', name: 'USD Coin', coingeckoId: 'usd-coin', isActive: true, minValueUsd: 50000 },
  { symbol: 'BNB', name: 'BNB', coingeckoId: 'binancecoin', isActive: true, minValueUsd: 10000 },
  { symbol: 'SOL', name: 'Solana', coingeckoId: 'solana', isActive: true, minValueUsd: 10000 },
  { symbol: 'ADA', name: 'Cardano', coingeckoId: 'cardano', isActive: true, minValueUsd: 10000 },
  { symbol: 'MATIC', name: 'Polygon', coingeckoId: 'matic-network', isActive: true, minValueUsd: 10000 }
];

async function main() {
  console.log('🌱 开始数据库种子数据初始化...');

  // 1. 初始化链配置
  console.log('📋 初始化链监控配置...');
  for (const config of chainConfigs) {
    await prisma.chainMonitorConfig.upsert({
      where: { chain: config.chain },
      update: {
        rpcUrl: config.rpcUrl,
        wssUrl: config.wssUrl,
        fallbackRpcs: config.fallbackRpcs,
        updatedAt: new Date()
      },
      create: config
    });
  }
  console.log(`✅ 已初始化 ${chainConfigs.length} 个链配置`);

  // 2. 初始化基础代币
  console.log('🪙 初始化基础监控代币...');
  for (const token of essentialTokens) {
    await prisma.monitoredToken.upsert({
      where: { symbol: token.symbol },
      update: {
        name: token.name,
        coingeckoId: token.coingeckoId,
        isActive: token.isActive,
        minValueUsd: token.minValueUsd,
        updatedAt: new Date()
      },
      create: {
        ...token,
        marketCapRank: essentialTokens.indexOf(token) + 1
      }
    });
  }
  console.log(`✅ 已初始化 ${essentialTokens.length} 个基础代币`);

  console.log('🎉 数据库种子数据初始化完成！');
  console.log('💡 如需完整的TOP1000代币数据，请运行: node scripts/init-top1000-tokens.js');
}

main()
  .catch((e) => {
    console.error('❌ 种子数据初始化失败:', e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  }); 